def getStudent(i):
    print("输入第",i,"个学生信息")
    try:
        Name=input("姓名：")
        if Name.strip()=="":
            raise Exception("无效姓名")
        Gender=input("性别:")
        if Gender!="男" and Gender!="女":
            raise Exception("无效性别")
        Age=input("年龄：")
        Age=int(Age)
        if Age<18 or Age>30:
            raise Exception("无效年龄")
        s={}
        s["Name"]=Name
        s["Gender"]=Gender
        s["Age"]=Age
        return s
    except Exception as err:
        print("err")
        return None
i=1
try:
    fobj=open("D:\\student.txt","wt")
    while True:
        s=getStudent(i)
        if s:
            fobj.write(s["Name"]+"\n"+s["Gender"]+"\n"+str(s["Age"])+"\n")
            i=i+1
        s=input("继续输入吗（Y/N）：")
        if s!="Y" and s!="y":
            break
    fobj.close()
except Exception as err:
    print("err")